
/**
 *	TrueMusic - simple java mp3 player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *	
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *	
 *	Copyright (c)2005-2006 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class TagDialog extends JDialog implements SwingConstants, ActionListener
{
	MP3File f;

	JTextField title=new JTextField();
	JTextField artist=new JTextField();
	JTextField album=new JTextField();
	JTextField track=new JTextField();

	JTextArea lyrics = new JTextArea();

	JButton save = new JButton("Save");
	JButton close = new JButton("Close");

	TagDialog(JFrame parent, AudioFile af)
	{
		super(parent, "Edit Tag for "+af.filename(), true );
		this.f = (MP3File)af;

		title.setText(f.getTitle());
		artist.setText(f.getArtist());
		album.setText(f.getAlbum());
		track.setText(f.getTrack());
		lyrics.setText(f.getLyrics());

		if( !new java.io.File(af.filename()).canWrite())
		{
			title.setEnabled(false);
			artist.setEnabled(false);
			album.setEnabled(false);
			track.setEnabled(false);
			lyrics.setEnabled(false);
			save.setEnabled(false);
		}

		setLayout(new BorderLayout());

		JPanel top = new JPanel( new GridLayout(4,2) );
		top.add( new JLabel("Title:",CENTER) );
		top.add( title);
		top.add( new JLabel("Artist:",CENTER) );
		top.add( artist);
		top.add( new JLabel("Album:",CENTER) );
		top.add( album);
		top.add( new JLabel("Track #:",CENTER) );
		top.add( track);


		JPanel bottom = new JPanel( new BorderLayout() );
		bottom.add( new JLabel("Lyrics:",CENTER), BorderLayout.NORTH );
		bottom.add( new JScrollPane(lyrics) );


		save.addActionListener(this);
		close.addActionListener(this);

		JPanel z = new JPanel();
		z.add(save);
		z.add(close);

		add(top, BorderLayout.NORTH);
		add(bottom);
		add( z, BorderLayout.SOUTH);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		setSize(400,400);
		setVisible(true);
	}

	public void dispose()
	{
		f=null;
		super.dispose();
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==save)
		{
			try
			{
				f.tag.setSongTitle( title.getText() );
				f.tag.setLeadArtist( artist.getText() );
				f.tag.setAlbumTitle( album.getText() );
				f.tag.setTrackNumberOnAlbum( track.getText() );
				f.tag.setSongLyric( lyrics.getText() );

				f.f3.save(org.farng.mp3.TagConstant.MP3_FILE_SAVE_OVERWRITE);
	
				f.readTag();
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(this, "Error writing tag for file\n"+f.filename()+"\n"+ex.toString(), "Error writing tag!", JOptionPane.ERROR_MESSAGE);
			}
		}
		if(e.getSource()==close)
		{
			dispose();
		}
	}
}
